import { submitOrder } from '@/service/order';
import { useUserStore } from '@/store/user';

const payRequest = (orderId, done) => {
  uni.getProvider({
    service: 'payment',
    success: async (res) => {
      const provider = res.provider[0];
      const userStore = useUserStore();
      const { data } = await submitOrder({
        channelCode: 'wx_pub',
        channelExtras: {
          openid: userStore.loginInfo.openid,
        },
        id: orderId,
      });
      console.log(data);
      const payConfig = JSON.parse(data.displayContent);
      uni.requestPayment({
        provider: provider,
        timeStamp: payConfig.timeStamp,
        nonceStr: payConfig.nonceStr,
        package: payConfig.packageValue,
        signType: payConfig.signType,
        paySign: payConfig.paySign,
        success: (result) => {
          uni.showToast({
            title: '支付成功',
            icon: 'success',
          })
          if (done) {
            done()
          }
         
        },
        fail: (error) => {
          if (error.errMsg === 'requestPayment:fail cancel') {
            uni.showToast({
              title: '支付取消',
              icon: 'none',
            })
          }
         
        }
      })
    }
  })
 
};

export { payRequest };
